/**
 * 
 */
package org.swing.utility.system.util;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
/**
 * @author Le Quynh Nhu
 *
 */
public class CommonUtils {

    public static final String APPLICATION_DIRECTORY_KEY = "application.directory";

    /**
     * Returns the user home directory.
     * 
     * @return the <tt>File</tt> instance denoting the abstract pathname of the
     *         user's home directory, or <tt>null</tt> if the home directory
     *         does not exist
     */
    public static File getUserHomeDir() {
        return new File(System.getProperty("user.home"));
    }

    /**
     * Return the user's name.
     * 
     * @return the <tt>String</tt> denoting the user's name.
     */
    public static String getUserName() {
        return System.getProperty("user.name");
    }

    /**
     * Returns the user's current working directory as a <tt>File</tt> instance,
     * or <tt>null</tt> if the property is not set.
     * 
     * @return the user's current working directory as a <tt>File</tt> instance,
     *         or <tt>null</tt> if the property is not set
     */
    public static File getCurrentDirectory() {
        return new File(System.getProperty("user.dir"));
    }

    /**
     * Returns the name of the directory that the application is installed into.
     * 
     * @return the application's directory
     */
    public static File getApplicationDirectory() {
        String applicationDirectory = System.getProperty(APPLICATION_DIRECTORY_KEY);
        if (applicationDirectory != null) {
            return new File(applicationDirectory);
        } else {
            return getCurrentDirectory();
        }
    }
    
//    public static File getApplicationPropertiesDirectory() {
//        File propertiesDirectory = new File(SystemUtils.getUserHome(), ".zooinspector");
//        if(!propertiesDirectory.isDirectory()) {
//                propertiesDirectory.mkdir();
//        }
//        return propertiesDirectory;
//    }
    

    /**
     * Gets an InputStream from a resource file.
     * 
     * @param location the location of the resource in the resource file
     * @return an <tt>InputStream</tt> for the resource
     * @throws IOException if the resource could not be located or there was
     *         another IO error accessing the resource
     */
    public static InputStream getResourceStream(String location) throws IOException {
        ClassLoader cl = CommonUtils.class.getClassLoader();
        URL resource = null;

        if (cl == null) {
            resource = ClassLoader.getSystemResource(location);
        } else {
            resource = cl.getResource(location);
        }

        if (resource == null)
            throw new IOException("null resource: " + location);
        else
            return resource.openStream();
    }
}